<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 活动接口
 */
class Activity extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 列表
     * 
     */
    public function index()
    {
        $res = Db::name('activity')->where('endtime','>',time())->select();
        foreach ($res as $k=>$v){
            $res[$k]['starttime'] = date('Y-m-d',$v['starttime']);
        }
        return json_encode($res);
    }

    public function detail(){
        $params = $this->request->post();
        $people = model('People')->people($params['openid']);
        $res = Db::name('activity')->where('id',$params['id'])->find();
        $res['starttime'] = date('Y-m-d H:i:s',$res['starttime']);
        $res['endtime'] = date('Y-m-d H:i:s',$res['endtime']);
        $stutus = Db::name('apply')->where(array('people_id'=>$people['id'],'activity_id'=>$res['id']))->count();
        $count = Db::name('apply')->where('activity_id',$res['id'])->count();
        return json_encode(['res'=>$res,'status'=>$stutus,'count'=>$count]);
    }
    public function bm(){
        $params = $this->request->post();
        $people = model('People')->people($params['openid']);
        $arr = array(
            'people_id'     => $people['id'],
            'activity_id'   => $params['id'],
            'createtime'    => time()
        );
        $res = Db::name('apply')->insert($arr);
        if($res){
            Db::name('activity')->where('id',$params['id'])->setInc('num');
            return 200;
        }else{
            return 400;
        }
    }
}
